﻿using ExcelParser.Writer;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace ExcelParser.CodeGen
{
    public abstract class ExcelField
    {
        public string Type { get; private set; }
        public string Name { get; set; }
        public string Desc { get; set; }

        public virtual int MaxChildrenCount { get { return 0; } }
        /// <summary>
        /// 父节点
        /// </summary>
        public ExcelField Parent { get; private set; }

        protected List<ExcelField> m_children;
        public ExcelField(string type, string name, string desc)
        {
            m_children = new List<ExcelField>();
            Type = type;
            Name = name;
            Desc = desc;
        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="child"></param>
        public void AddChild(ExcelField child)
        {
            if (child == null)
                return;

            if (m_children.Count >= MaxChildrenCount)
                return;

            child.Parent = this;
            m_children.Add(child);
        }
        /// <summary>
        /// 生成类型
        /// </summary>
        /// <param name="writer"></param>
        public virtual void GenClass(CodeWriter writer) { }
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="writer"></param>
        public virtual void GenCode(CodeWriter writer) { }

        /// <summary>
        /// 解析配置数据
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="value"></param>
        public abstract void ParseValue(CodeWriter writer, object obj, PropertyInfo propertyInfo, MethodInfo methodInfo, Type fieldType, string value);
    }
}